<?php
include 'includes/application_top.inc';
//print_r($_REQUEST);
    $id = @$_GET['id'];
   // determine if short url is valid
    $query = "SELECT id,long_url,shorturl_code FROM shorturl WHERE shorturl_code = '". $id ."'"; 
   //$stringdd = "SELECT url FROM redirects WHERE string = '". $string ."'";
	$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
	$row = mysql_fetch_array($result);
	$long_url = $row['long_url'];
	$url_id = $row['id'];
	$shorturl_code = $row['shorturl_code'];
	 
	 
	$num_results = mysql_num_rows($result);
   if ( $num_results > 0  ) {
    header("HTTP/1.1 301 Moved Permanently");
	$today = date("Y-m-d H:i:s");
	$msisdn=@$_SERVER['HTTP_MSISDN'];			    
	$httpuseragent=$_SERVER['HTTP_USER_AGENT'];
	$arr = explode("(",$httpuseragent);
	$userAgent=  $arr[0];
	$arrnext = explode(")",$arr[1]);
    $os = $arrnext[0];
	//print_r($arrnext[0]);
	//die();		
	echo $query = "INSERT INTO track_url(t_id,msisdn,useragent,os,datetime,shorturl_id,shorturl_code,long_url) values(NULL,'$msisdn','$userAgent','$os','$today','$url_id','$shorturl_code','$long_url')";
    $result = mysql_query($query) or die ("Error in query: $query " . mysql_error());	   
	// die();
    header("Location: ". $long_url );
	exit;
   } else {
    // redirect to submit page or short URL not found page.
	
	//echo "amar...........".$num_results;
   }
mysql_close($con);
 ?> 